﻿/*
 * Copyright (C) 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using UnityEngine;
using System.Collections;

public class HUDRenderer : MonoBehaviour {
    public GUISkin HudSkin;
    public GameObject PlayerObj;
    public Texture2D FireButtonTex;

    private PlayerAimController aimController;
    private FireController fireController;

    private int scoreShown = 0;
    private const int ScoreShownInc = 1;

    void Start() {
        aimController = PlayerObj.GetComponent<PlayerAimController>();
        fireController = PlayerObj.GetComponent<FireController>();
    }

    void OnGUI() {
        if (GameManager.Instance.Mode != GameManager.GameMode.Playing) return;

        GUI.skin = HudSkin;

        int azi = (int)aimController.Azimuth;
        string aziStr = Utils.IntWithLeadingZeroes(3, azi);

        int elev = (int)aimController.NormalizedElevation;
        string elevStr = Utils.IntWithLeadingZeroes(2, elev);

        int score = GameManager.Instance.Score;
        if (Mathf.Abs(scoreShown - score) < ScoreShownInc) scoreShown = score;
        else if (scoreShown < score) scoreShown += ScoreShownInc;
        else scoreShown -= ScoreShownInc;
        string scoreStr = Utils.IntWithLeadingZeroes(5, scoreShown);

        // score
        Gu.Label(Gu.Left(20), Gu.Top(10), Gu.Dim(30), "score");
        Gu.Label(Gu.Left(20), Gu.Top(40), Gu.Dim(60), scoreStr);

        // azimuth
        Gu.Label(Gu.Right(150), Gu.Top(20), Gu.Dim(30), "azimuth");
        Gu.Label(Gu.Right(150), Gu.Top(50), Gu.Dim(60), aziStr);

        // elevation
        Gu.Label(Gu.Right(150), Gu.Top(140), Gu.Dim(30), "elevation");
        Gu.Label(Gu.Right(150), Gu.Top(170), Gu.Dim(60), elevStr);

        // fire buttons
        const float ButtonWidth = 350;
        const float ButtonHeight = ButtonWidth / 2;
        const float ButtonMargin = 0;
        bool fire = false;

        // right-hand side fire button
        fire = Gu.Button(Gu.Right(ButtonWidth + ButtonMargin), Gu.Bottom(ButtonHeight + ButtonMargin),
            Gu.Dim(ButtonWidth), Gu.Dim(ButtonHeight), FireButtonTex);

        // left-hand side fire button
        fire = fire || Gu.Button(Gu.Left(ButtonMargin), Gu.Bottom(ButtonHeight + ButtonMargin),
            Gu.Dim(ButtonWidth), Gu.Dim(ButtonHeight), FireButtonTex);

        if (fire) {
            fireController.FireMissile();
        }
    }
}
